package teamproject.visualization.gui.visualobjects;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.RenderingAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;

public class TransparentEdge implements VisualObject {

	private float length = 0;
	private int parts = 0;
	private float radius = 0;
	private ArrayList<CustomCylinder> listOfParts = new ArrayList<CustomCylinder>();
	private Appearance app;
	private RenderingAttributes ra;

	public TransparentEdge(float length, int numberOfParts) {
		this(length, numberOfParts, 1.0f);
	}

	public TransparentEdge(float length, int numberOfParts, float radius) {
		this.setLength(length);
		this.setNumberOfParts(numberOfParts);
		this.setRadius(radius);
		ra = new RenderingAttributes();
		ra.setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
		app = createAppearance2();

		for (int i = 0; i < parts; i++) {

			CustomCylinder cyc = new CustomCylinder(radius,
					(length / parts), Primitive.GENERATE_NORMALS, app, i);

			cyc.setAppearance(Cylinder.BODY, createAppearance());
			if (i == 0) {
				cyc.setAppearance(Cylinder.BOTTOM, createAppearance());
			}
			if (i == parts - 1) {
				cyc.setAppearance(Cylinder.TOP, createAppearance());
			}

			listOfParts.add(cyc);
		}
	}

	private Appearance createAppearance() {
		Appearance app = new Appearance();
		app.setRenderingAttributes(ra);
		TransparencyAttributes ta = new TransparencyAttributes();
		ta.setTransparencyMode(TransparencyAttributes.BLENDED);
		ta.setTransparency(0.5f);
		app.setTransparencyAttributes(ta);

		// color
		ColoringAttributes caColor = new ColoringAttributes();
		caColor.setColor(0.5f, 1.0f, 0.3f);

		caColor.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
		app.setColoringAttributes(caColor);

		// Light properties
		Color3f objColor = new Color3f(0.91f, 0.27f, 0.72f);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));

		return app;
	}

	private Appearance createAppearance2() {
		Appearance app = new Appearance();

		app.setRenderingAttributes(ra);

		TransparencyAttributes ta = new TransparencyAttributes();
		ta.setTransparencyMode(TransparencyAttributes.BLENDED);
		ta.setTransparency(1.0f);
		app.setTransparencyAttributes(ta);
		// color
		ColoringAttributes caColor = new ColoringAttributes();
		caColor.setColor(0.91f, 0.27f, 0.72f);

		app.setColoringAttributes(caColor);

		return app;
	}

	public float getLength() {
		return length;
	}

	public int getNumberOfParts() {
		return parts;
	}

	public ArrayList<CustomCylinder> getParts() {
		return listOfParts;
	}

	public float getRadius() {
		return radius;
	}

	@Override
	public boolean isVisible() {
		return ra.getVisible();
	}

	public void setLength(float length) {
		this.length = length;
	}

	public void setNumberOfParts(int parts) {
		this.parts = parts;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	@Override
	public void setVisible(boolean visible) {
		ra.setVisible(visible);
	}
}
